﻿using DTOModels.User;
using System.Security.Claims;

namespace AuthServer.JwtService
{
    /// <summary>
    /// 颁发Ttoken的抽象类
    /// </summary>
    public abstract class CustomJWTService
    {
        /// <summary>
        /// 抽象函数，生成Token的，具体实现，留给实现类来实现---多种方式来实现
        /// </summary>
        /// <param name="user">SysUserInfo，从数据库中查询出来以后</param>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public abstract string CreateToken(SysUserInfo user, out string refreshToken);

        /// <summary>
        /// 生成token 准备用户的信息，写入到Claim
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        protected virtual List<Claim> UserToClaim(SysUserInfo user)
        {
            //准备有效载荷
            List<Claim> claimsArray = new List<Claim>()
            {
               new Claim(ClaimTypes.Sid, user.UserId.ToString()),
               new Claim(ClaimTypes.Name, user.Name?? string.Empty),
               new Claim(ClaimTypes.MobilePhone, user.Mobile?? string.Empty),
               new Claim(ClaimTypes.OtherPhone, user.Phone?? string.Empty),
               new Claim(ClaimTypes.StreetAddress, user.Address?? string.Empty),
               new Claim(ClaimTypes.Email, user.Email?? string.Empty),
               new Claim("QQ", user.QQ.ToString()),
               new Claim("WeChat", user.WeChat?? string.Empty),
               new Claim("Sex", user.Sex.ToString())
            };
            foreach (var roleId in user.RoleIdList!)
            {
                claimsArray.Add(new Claim(ClaimTypes.Role, roleId.ToString()));
            }
            foreach (var menu in user.UserMenuList!)
            {
                claimsArray.Add(new Claim("Menus", Newtonsoft.Json.JsonConvert.SerializeObject(menu)));
            }
            foreach (var btn in user.UserBtnList!)
            {
                claimsArray.Add(new Claim("Btns", btn.Value));
            }
            return claimsArray;
        }
    }
}
